package com.ruoyi.zxjd.demeanor.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.zxjd.common.CommonConstants;
import com.ruoyi.zxjd.demeanor.domain.DemeanorArrange;
import com.ruoyi.zxjd.demeanor.service.IDemeanorArrangeService;
import com.ruoyi.zxjd.document.domain.DocumentFile;
import com.ruoyi.zxjd.document.service.IDocumentFileService;
import com.ruoyi.zxjd.util.AimPathUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 纪检风采Controller
 *
 * @author lcc
 * @date 2021-03-16
 */
@Controller
@RequestMapping("/demeanor/arrange")
public class DemeanorArrangeController extends BaseController {
    private String prefix = "demeanor/arrange";

    @Autowired
    private IDemeanorArrangeService demeanorArrangeService;

    @Autowired
    private IDocumentFileService documentFileService;

    //@RequiresPermissions("demeanor:arrange:view")
    @GetMapping("/{demeanorCategory}")
    public String arrange(@PathVariable("demeanorCategory") String demeanorCategory, ModelMap mmap) {
        mmap.put("demeanorCategory", demeanorCategory);
        return prefix + "/arrange";
    }

    /**
     * 查询纪检风采列表
     */
    @RequiresPermissions("demeanor:arrange:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DemeanorArrange demeanorArrange) {

        if (demeanorArrange != null && demeanorArrange.getDemeanorCategory() != null && demeanorArrange.getDemeanorCategory().equals("5")) {
            demeanorArrange.setDemeanorCategory(null);
        }

        startPage();
        //查询结果,没有包含图片路径
        List<DemeanorArrange> list = demeanorArrangeService.selectDemeanorArrangeList(demeanorArrange);


        return getDataTable(list);
    }

    /**
     * 导出纪检风采列表
     */
    @RequiresPermissions("demeanor:arrange:export")
    @Log(title = "纪检风采", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DemeanorArrange demeanorArrange) {
        List<DemeanorArrange> list = demeanorArrangeService.selectDemeanorArrangeList(demeanorArrange);
        ExcelUtil<DemeanorArrange> util = new ExcelUtil<DemeanorArrange>(DemeanorArrange.class);
        return util.exportExcel(list, "arrange");
    }

    /**
     * 新增纪检风采
     */
    @GetMapping("/add")
    public String add(String demeanorCategory, ModelMap mmap) {
        if (demeanorCategory != null && demeanorCategory.equals("5")) {
            demeanorCategory = null;
        }
        mmap.put("demeanorCategory", demeanorCategory);
        return prefix + "/add";
    }

    /**
     * 新增保存纪检风采
     */
    @RequiresPermissions("demeanor:arrange:add")
    @Log(title = "纪检风采", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DemeanorArrange demeanorArrange) {
        return toAjax(demeanorArrangeService.insertDemeanorArrange(demeanorArrange));
    }

    /**
     * 修改纪检风采
     */
    @GetMapping("/edit/{arrangeId}")
    public String edit(@PathVariable("arrangeId") Long arrangeId, ModelMap mmap) {
        DemeanorArrange demeanorArrange = demeanorArrangeService.selectDemeanorArrangeById(arrangeId);
        mmap.put("demeanorArrange", demeanorArrange);
        return prefix + "/edit";
    }


    /**
     * 修改保存纪检风采
     */
    @RequiresPermissions("demeanor:arrange:edit")
    @Log(title = "纪检风采", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DemeanorArrange demeanorArrange) {
        return toAjax(demeanorArrangeService.updateDemeanorArrange(demeanorArrange));
    }

    /**
     * 删除纪检风采
     */
    @RequiresPermissions("demeanor:arrange:remove")
    @Log(title = "纪检风采", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(demeanorArrangeService.deleteDemeanorArrangeByIds(ids));
    }


    /**
     * 发布工作
     */
    @RequiresPermissions("work:arrange:release")
    @Log(title = "工作安排", businessType = BusinessType.UPDATE)
    @PostMapping("/release")
    @ResponseBody
    public AjaxResult release(Long arrangeId) {
        return toAjax(demeanorArrangeService.updateStatusByArrangeId(arrangeId, CommonConstants.ONGOING));
    }

    /**
     * 撤销工作
     */
    @RequiresPermissions("work:arrange:withdraw")
    @Log(title = "工作安排", businessType = BusinessType.UPDATE)
    @PostMapping("/withdraw")
    @ResponseBody
    public AjaxResult withdraw(Long arrangeId) {
        return toAjax(demeanorArrangeService.updateStatusByArrangeId(arrangeId, CommonConstants.DRAFT));
    }

    /**
     * 个人纪检风采展示页面,
     * 查询列表数据
     *
     * @param demeanorArrange
     * @return
     */
/*    @PostMapping("/personal/list")
    public String arrange(DemeanorArrange demeanorArrange, ModelMap mmap) {



        return prefix + "/person/person_arrange";
    }*/

    /**
     * 跳转到个人纪检风采展示页面
     */
    @GetMapping("/personal/page")
    public String personList(@RequestParam("demeanorCategory") String demeanorCategory, Integer offset, Integer limit, String fragment, ModelMap mmap) {
        String aimPath = prefix + "/person/person_arrange";

        DemeanorArrange demeanorArrange = new DemeanorArrange();
        demeanorArrange.setDemeanorCategory(demeanorCategory);
        demeanorArrange.setStatus("2");
        List<DemeanorArrange> list = demeanorArrangeService.selectDemeanorArrangeWithPicPathListAndPage(demeanorArrange, offset, limit);
        mmap.put("list", list);
        mmap.put("demeanorCategory", demeanorCategory);
        aimPath = AimPathUtils.getAimPath(aimPath, fragment);
        return aimPath;
    }


    /**
     * 个人页面, 查看纪检风采详情
     */
    @RequiresPermissions("document:progress:detail")
    @GetMapping("/personal/detail/{arrangeId}")
    public String detail(@PathVariable("arrangeId") Long arrangeId, ModelMap mmap) {
        DemeanorArrange demeanorArrange = demeanorArrangeService.selectDemeanorArrangeById(arrangeId);
        mmap.put("demeanorArrange", demeanorArrange);

        Long[] ids = (Long[]) ConvertUtils.convert(demeanorArrange.getAttachments().split(","), Long.class);
        List<DocumentFile> documentFileList = documentFileService.selectDocumentFileByIds(ids);
        mmap.put("documentFileList", documentFileList);
        mmap.put("createTime", DateUtils.dateAndTime(demeanorArrange.getCreateTime()));
        return prefix + "/person/person_detail";
    }


    /**
     * 修改纪检风采
     */
    @GetMapping("/view/video")
    public String edit(@RequestParam("url") String url, ModelMap mmap) {
        mmap.put("videoUrl", url);
        return  prefix + "/person/video_show";
    }


}
